package com.tim.framework.util.properties;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * 资源文件操作抽象类.
 * 
 * @author Tim.
 */
public abstract class AbsPropertiesProc implements IPropertiesProc {
	private static Logger log = Logger.getLogger(AbsPropertiesProc.class);
	/**
	 * 资源文件缓存.
	 */
	private Map<String, Properties> PROPERTIES_CACHE = new HashMap<String, Properties>();

	/**
	 * 设置资源对象.
	 * 
	 * @param file
	 *            资源文件.
	 */
	public void setPropertiesUseFile(File file) {
		try {
			if (PROPERTIES_CACHE.get(file.getAbsolutePath()) == null) {
				InputStream in = new BufferedInputStream(new FileInputStream(
						file));
				Properties props = new Properties();
				props.load(in);
				PROPERTIES_CACHE.put(file.getAbsolutePath(), props);
			}
		} catch (FileNotFoundException ex) {
			log.error("try to set properties use file["
					+ file.getAbsolutePath() + "] ERROR：file not found.");
		} catch (IOException ex) {
			log.error("try to creat the properties use file["
					+ file.getAbsolutePath() + "] ERROR：IOException.");
		}
	}

	/**
	 * 设置资源对象.
	 * 
	 * @param filePath
	 *            资源文件路径.
	 */
	public void setPropertiesUsePath(String filePath) {
		setPropertiesUseFile(new File(filePath));
	}

	/**
	 * 获取资源文件缓存.
	 */
	public Map<String, Properties> getPropertiesCache() {
		return this.PROPERTIES_CACHE;
	}
}
